<template>
  <el-row :gutter="10">
    <el-col :span="5">
      <el-card>
        <OrgTree authority @node-click="handleOrgClick" />
      </el-card>
    </el-col>
    <el-col :span="19">
      <el-card class="layout-query">
        <el-form
          ref="queryRef"
          :inline="true"
          :model="state.queryForm"
          @keyup.enter="getDataList()">
          <el-form-item prop="key">
            <el-input v-model="state.queryForm.key" placeholder=""></el-input>
          </el-form-item>
<!--          <el-form-item prop="telephone">-->
<!--            <el-input v-model="state.queryForm.telephone" placeholder="电话"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item prop="idNo">-->
<!--            <el-input v-model="state.queryForm.idNo" placeholder="身份证号"></el-input>-->
<!--          </el-form-item>-->
          <!-- <el-form-item label="所属车队" prop="teamId">
            <el-select v-model="state.queryForm.teamId" placeholder="请选择" clearable>
              <el-option v-for="car in carList" :key="car.id" :label="car.name" :value="car.id" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <el-space>
          <el-space>
            <el-button
              v-auth="'biz:driver:save'"
              icon="Plus"
              type="primary"
              @click="addOrUpdateHandle()"
              >新增</el-button
            >
          </el-space>
          <el-space>
            <el-button
              v-auth="'biz:driver:delete'"
              icon="Delete"
              plain
              type="danger"
              @click="deleteBatchHandle()"
              >批量删除</el-button
            >
          </el-space>
        </el-space>
        <el-table
          v-loading="state.dataListLoading"
          :data="state.dataList"
          border
          class="layout-table"
          @selection-change="selectionChangeHandle">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"></el-table-column>
          <!-- <el-table-column
            prop="id"
            label=""
            header-align="center"
            align="center"></el-table-column> -->
          <el-table-column
            prop="sysOrgName"
            label="所属公司/车队"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="telephone"
            label="电话"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="idNo"
            label="身份证号"
            header-align="center"
            align="center"></el-table-column>
          <!-- <el-table-column
            prop="licensePhoto"
            label="驾驶证照片"
            header-align="center"
            align="center">
            <template #default="scope">
              <img :src="scope.row.licensePhoto" style="width: 40px; height: 40px" alt="" />
            </template>
          </el-table-column> -->
          <!-- <el-table-column
            prop="teamName"
            label="所属车队"
            header-align="center"
            align="center"></el-table-column> -->
          <el-table-column
            label="操作"
            fixed="right"
            header-align="center"
            align="center"
            width="150">
            <template #default="scope">
              <el-button
                v-auth="'biz:driver:update'"
                type="primary"
                link
                @click="addOrUpdateHandle(scope.row.id)"
                >修改</el-button
              >
              <el-button
                v-auth="'biz:driver:delete'"
                type="primary"
                link
                @click="deleteBatchHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="state.page"
          :page-sizes="state.pageSizes"
          :page-size="state.limit"
          :total="state.total"
          layout="total, sizes, prev, pager, next"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle">
        </el-pagination>

        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts" name="BizDriverIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './add-or-update.vue'
import OrgTree from '@/views/sys/user/org-tree.vue'
import { useTeamListApi } from '@/api/biz/team'

const state: IHooksOptions = reactive({
  dataListUrl: '/biz/driver/page',
  deleteUrl: '/biz/driver',
  queryForm: {
    key: '',
    // telephone: '',
    // idNo: '',
    // teamId: '',
    sysOrgId: '',
    order: 'create_time',
    asc: false
  }
})
const carList = ref<any[]>([])
const queryRef = ref()
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id)
}

const handleOrgClick = (orgId: any) => {
  state.queryForm.sysOrgId = orgId
  getDataList()
}

function getTeamList() {
  useTeamListApi(state.queryForm).then((res) => {
    carList.value = res.data
  })
}
getTeamList()
const {
  getDataList,
  selectionChangeHandle,
  sizeChangeHandle,
  currentChangeHandle,
  deleteBatchHandle,
  reset
} = useCrud(state)
</script>
